package texture;
import opengl.renderer.GLRenderer;
import opengl.renderer.LSRenderer;
import opengl.renderer.RendererFactory;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

import color.GLColors;
import font.ColorEffect;
import font.GLFontUnicode;

public class FontUnicodeTest {
	private GLRenderer GL = RendererFactory.getGLRendererImmediate();

	public static void main(String[] args) {
		System.out.println("starting");
		FontUnicodeTest fontExample = new FontUnicodeTest();
		fontExample.start();
	}
	/** The font we're going to display in the test */
	private GLFontUnicode unicodeFont1;
	private GLFontUnicode unicodeFont2;

	/**
	 * /** Start the test
	 */
	public void start() {
		initGL(800, 600);
		init();

		while (true) {
			GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
		
			renderLine();
			renderLineQuad();
			render();

			Display.update();
			Display.sync(100);

			if (Display.isCloseRequested()) {
				Display.destroy();
				System.exit(0);
			}
		}
	}

	/**
	 * Initialise the GL display
	 * 
	 * @param width
	 *            The width of the display
	 * @param height
	 *            The height of the display
	 */
	private void initGL(int width, int height) {
		try {
			Display.setDisplayMode(new DisplayMode(width, height));
			Display.create();
			Display.setVSyncEnabled(true);
		} catch (LWJGLException e) {
			e.printStackTrace();
			System.exit(0);
		}

		GL11.glEnable(GL11.GL_TEXTURE_2D);
		GL11.glShadeModel(GL11.GL_SMOOTH);
		GL11.glDisable(GL11.GL_DEPTH_TEST);
		GL11.glDisable(GL11.GL_LIGHTING);

		GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		GL11.glClearDepth(1);

		GL11.glEnable(GL11.GL_BLEND);
		GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

		GL11.glViewport(0, 0, width, height);
		GL11.glMatrixMode(GL11.GL_MODELVIEW);

		GL11.glMatrixMode(GL11.GL_PROJECTION);
		GL11.glLoadIdentity();
		GL11.glOrtho(0, width, height, 0, 1, -1);
		GL11.glMatrixMode(GL11.GL_MODELVIEW);
	}

	/**
	 * Initialise resources
	 */
	public void init() {
		// load a default java font
		try {

			unicodeFont1 = new GLFontUnicode("assets/font/STXINGKA.TTF", 32, false, false);
			unicodeFont2 = new GLFontUnicode("assets/font/wts11.ttf", 32, false, false);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		unicodeFont1.getEffects().add(new ColorEffect(java.awt.Color.white));
		unicodeFont2.getEffects().add(new ColorEffect(java.awt.Color.white));
	}

	/**
	 * Game loop render
	 */
	public void render() {
		GL.glColor(GLColors.WHITE);
		// unicodeFont.addGlyphs("那只敏捷的棕色狐狸");
		try {

			float xpos = 150.0f;
			float ypos = 150.0f;
			unicodeFont1.loadGlyphs(1);
			unicodeFont1.drawString(xpos, ypos, "那只敏捷的棕色狐狸12345", GLColors.WHITE); // renders the font

			float xpos2 = 250.0f;
			float ypos2 = 250.0f;
			unicodeFont2.loadGlyphs(1);
			unicodeFont2.drawString(xpos2, ypos2, "那只敏捷的棕色狐狸34567", GLColors.WHITE); // renders the font
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void renderLine() {
		LSRenderer r = RendererFactory.getLSRendererBase();
		r.start();
		r.color(10, 10, 10, 1);
		r.vertex(100, 100);
		r.vertex(200, 200);
		r.end();
	}

	public void renderLineQuad() {
		LSRenderer r = RendererFactory.getLSRendererQuad();
		r.start();
		r.setWidth(50);
		r.color(10, 10, 10, 1);
		r.vertex(100, 100);
		// r.vertex(100, 105);
		r.vertex(600, 100);
		// r.vertex(400, 105);
		r.end();
	}

}
